package com.hq.fxh.Utils;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import java.io.File;

/**
 * Created by 大圣  on 2015/7/22 0022.
 */
public abstract class IntentUtils {
    /**
     * 拨打电话
     * To phone app.
     *
     * @param context the context
     * @param tel the tel
     */
    public static void toPhoneApp(Context context, String tel) {
        Uri uri = Uri.parse("tel:" + tel);
        Intent it = new Intent(Intent.ACTION_DIAL, uri);
        context.startActivity(it);
    }

    /**
     * 调用发短信程序
     * To sms app.
     *
     * @param context the context
     * @param text the text
     */
    public static void toSMSApp(Context context, String text) {
        Intent it = new Intent(Intent.ACTION_VIEW);
        it.putExtra("sms_body", text);
        it.setType("vnd.android-dir/mms-sms");
        context.startActivity(it);
    }

    /**
     * 发送短信
     * To sms app.
     *
     * @param context the context
     * @param tel the tel
     * @param text the text
     */
    public static void toSMSApp(Context context, String tel, String text) {
        Uri uri = Uri.parse("smsto:" + tel);
        Intent it = new Intent(Intent.ACTION_SENDTO, uri);
        it.putExtra("sms_body", text);
        context.startActivity(it);
    }

    /**
     * 卸载apk
     * Uninstall apk.
     *
     * @param context the context
     * @param strPackageName the str package name
     */
    public static void uninstallApk(Context context, String strPackageName) {
        Uri uri = Uri.fromParts("package", strPackageName, null);
        Intent it = new Intent(Intent.ACTION_DELETE, uri);
        context.startActivity(it);
    }

    /**
     * 安装apk
     * Install apk.
     *
     * @param context the context
     * @param strPackageName the str package name
     */
    public static void installApk(Context context, String strPackageName) {
        Uri installUri = Uri.fromParts("package", strPackageName, null);
        Intent it = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
        context.startActivity(it);
    }
    public static void installApk(Context context , File apkFile){
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

    /**
     * 打开另一个程序
     * Open other apk.
     *
     * @param context the context
     * @param appPackageName the app package name
     * @param RESULT_OK the result ok
     */
    public static void openOtherApk(Activity context, String appPackageName, int RESULT_OK) {
        Intent i = new Intent();
        ComponentName cn = new ComponentName(context.getApplicationContext().getPackageName(),
                appPackageName);
        i.setComponent(cn);
        i.setAction("android.intent.action.MAIN");
        context.startActivityForResult(i, RESULT_OK);
    }
    /**
     * 打开网络设置界面
     *
     * @param context
     */
    public static void openNetSetting(Activity context){
        Intent intent = null;
        if(android.os.Build.VERSION.SDK_INT > 10){
            intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
        }else{
            intent = new Intent();
            ComponentName component = new ComponentName("com.android.settings","com.android.settings.WirelessSettings");
            intent.setComponent(component);
            intent.setAction("android.intent.action.VIEW");
        }
        context.startActivity(intent);

    }
    /**
     * 跳转界面
     *
     * @param pClass 需要跳转到的类
     */
    public static void openActivity(Context context, Class<?> pClass) {
        openActivity(context,pClass, null);
//        /** 页面跳转动画 */
//        context.overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
    }
    /**
     * 跳转界面
     *
     * @param pClass 需要跳转到的类
     * @param pBundle 携带的参数
     */
    public static void openActivity(Context context, Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(context, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
         }
        context.startActivity(intent);
    }
    /**
     * 跳转界面
     *
     * @param pAction action
     */
    public static void openActivity(Context context , String pAction) {
        openActivity(context, pAction, null);
    }

    /**
     * 跳转界面
     *
     * @param pAction action
     * @param pBundle 携带参数
     */
    public static void openActivity(Context context , String pAction, Bundle pBundle) {
        Intent intent = new Intent(pAction);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        context.startActivity(intent);
    }

    /**
     *
     * @param context
     * @param pClass
     * @param requestCode
     * @param pBundle
     */
    public static void openActivityForResult(Activity context, Class<?> pClass, int requestCode, Bundle pBundle){
        Intent intent = new Intent(context,pClass);
        if(pBundle != null){
            intent.putExtras(pBundle);
        }
        context.startActivityForResult(intent, requestCode);
    }
    /**
     *
     * @param context
     * @param pClass
     * @param requestCode
     */
    public static void openActivityForResult(Activity context, Class<?> pClass, int requestCode){
        Intent intent = new Intent(context,pClass);
        context.startActivityForResult(intent, requestCode);
    }

    /**
     * 跳转到微信
     */
    public static void openToWeiXin(Activity context){

        Intent intent = new Intent();

        ComponentName cmp = new ComponentName("com.tencent.mm","com.tencent.mm.ui.LauncherUI");
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setComponent(cmp);
        context.startActivityForResult(intent, 0);

    }
    /**
     * 分享功能
     *
     * @param context
     *            上下文
     * @param activityTitle
     *            Activity的名字
     * @param msgTitle
     *            消息标题
     * @param msgText
     *            消息内容
     * @param imgPath
     *            图片路径，不分享图片则传null
     */
    public void shareMsg(Context context , String activityTitle, String msgTitle, String msgText,
                         String imgPath) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        if (imgPath == null || imgPath.equals("")) {
            intent.setType("text/html"); // 纯文本
        } else {
            File f = new File(imgPath);
            if (f != null && f.exists() && f.isFile()) {
                intent.setType("image/jpg");
                Uri u = Uri.fromFile(f);
                intent.putExtra(Intent.EXTRA_STREAM, u);
            }
        }
        intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
        intent.putExtra(Intent.EXTRA_TEXT, msgText);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(Intent.createChooser(intent, activityTitle));
    }
}
